
.def PRINT_A prn
.def INC_CARRY(r1, r2) in%r2, jnc { in%r1, },

.org $0000                 ; Start of ROM

	lxy #main_program      ; Set up program entry point
	xxi
	
	lxy #stack_page        ; Set up stack
	xxs
	
	run                    ; Allow program to run on interrupt exit

interrupt_routine:
	rti                    ; Exit interrupt into main program
	
	pha                    ; Save accumulator on stack
	
	lda #'i'               ; Print 'i'
	PRINT_A
	
	ppa                    ; Restore accumulator
	jpr interrupt_routine  ; Loop back to beginning for next interrupt

main_program:              ; Entry point into user code
	
	lxy #hello_world       ; Call print_string function with the string pointer in XY
	jsr print_string
	
	hlt                    ; Stop user execution

print_string:              ; Function to print a null-terminated string, whose address is in XY
	lda xy                 ; Load next char
	jpz print_string_done  ; Exit if null
	PRINT_A                ; Print char
	INC_CARRY(x, y)
	jpr print_string       ; Continue to next char
print_string_done:
	rts                    ; Return

hello_world:
	"Hello, world!\n\0"    ; String to print, null-terminated

.org $8000                 ; Start of RAM
.aln $0100                 ; Allocate one page for the stack to grow upward
stack_page:
	byte[$0100]
